package com.kob.backend.service.impl.user.account;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kob.backend.mapper.UserMapper;
import com.kob.backend.pojo.User;
import com.kob.backend.service.user.account.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class RegisterServiceImpl implements RegisterService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public Map<String, String> register(String username, String password, String confirmedPassword) {
        Map<String, String> map = new HashMap<>();
        if(username == null||username.equals("")){
            map.put("message","用户名不能为空");
            return map;
        }
        if(password == null || confirmedPassword == null){
            map.put("message","密码不能为空");
            return map;
        }
        username = username.trim();
        password = password.trim();
        confirmedPassword = confirmedPassword.trim();
        if(username.contains(" ")||password.contains(" ")){
            map.put("message","用户名或密码不能包含空格");
            return map;
        }
        if(username.length()>20){
            map.put("message","用户名过长");
            return map;
        }
        if(password.length() < 3){
            map.put("message","密码太短");
            return map;
        }
        if(!password.equals(confirmedPassword)){
            map.put("message","两次输入的密码不一致");
            return map;
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        User user = userMapper.selectOne(queryWrapper);
        if(user!=null){
            map.put("message","用户名已存在");
            return map;
        }

        String encodePassword = passwordEncoder.encode(password);
        User user1 = new User(null, username, encodePassword, "https://s1.ax1x.com/2023/02/19/pSLch79.jpg",1500);
        userMapper.insert(user1);
        map.put("message","注册成功");
        return map;
    }
}
